/*
 * Copyright 2018-10-21 8:24:50 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.erp.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.admin.utils.DictionaryUtils;
import com.lap.scp.erp.ao.AttendanceAO;
import com.lap.scp.erp.bean.AttendanceBean;
import com.lap.scp.erp.domain.AttendanceDO;
import com.lap.scp.erp.query.AttendanceQueryVO;

/**
 * <pre>
 *  出勤记录控制对象，提供 出勤记录 新增,编辑,查询等操作。
 *  控制层统一命名规则：
 *  进入新增 	add,
 *  增加保存 	save,
 *  进入修改 	edit,
 *  修改保存 	update,
 *  查询分页 	queryPage,
 *  根据ID查询 	get,
 *  删除 		deleteById
 *
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2018-10-21 8:24:50
 * @since 2.0.0
 */
@Controller
@RequestMapping("attendance")
public class AttendanceController {
	/**
	 * 出勤记录应用流程对象
	 */
	@Resource
	private AttendanceAO attendanceAO;

	/**
	 * <pre>
	 * 进入新增出勤记录
	 * </pre>
	 * 
	 * @return
	 */
	@GetMapping(value = "add")
	public ModelAndView add() {
		ModelAndView andView = new ModelAndView("erp/attendanceAdd");
		andView.addObject("attendanceDO", new AttendanceDO());
		return andView;
	}

	/**
	 * <pre>
	 * 新增保存出勤记录
	 * </pre>
	 * 
	 * @param attendanceDO
	 * @param error
	 * @return
	 */
	@PostMapping(value = "addBath")
	@ResponseBody
	public Resp<Integer> addBath(AttendanceBean bean) {
		return attendanceAO.insertBatch(bean);
	}

	/**
	 * <pre>
	 * 根据ID删除出勤记录
	 * </pre>
	 * 
	 * @param attendanceId
	 * @param map
	 * @param attributes
	 * @return
	 */
	@GetMapping(value = "delete/{attendanceId}")
	public ModelAndView deleteById(@PathVariable Integer attendanceId, @RequestParam Map<String, Object> map,
			RedirectAttributes attributes) {
		ModelAndView andView = new ModelAndView();

		Resp<Integer> resp = attendanceAO.deleteById(attendanceId);
		if (resp.isSuccess()) {
			andView.setViewName("redirect:/attendance/list");
			attributes.addAllAttributes(map);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 根据ID查询出勤记录
	 * // TODO 生成的代码，如果确认你已经使用该方法，请删除该行注释，不然后续清理代码的时候，将会删除该方法
	 * </pre>
	 * 
	 * @param attendanceId
	 * @return
	 */
	@GetMapping(value = "get/{attendanceId}")
	public ModelAndView get(@PathVariable Integer attendanceId) {
		ModelAndView andView = new ModelAndView();
		Resp<AttendanceDO> resp = attendanceAO.queryById(attendanceId);
		if (resp.isSuccess()) {
			andView.setViewName("attendanceDetail");
			andView.addObject("attendanceDO", resp.getData());
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 根据条件、分页信息查询出勤记录
	 * </pre>
	 * 
	 * @param page
	 * @param queryVO
	 * @return
	 */
	@RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView queryPage(Page page, AttendanceQueryVO queryVO) {
		ModelAndView andView = new ModelAndView();

		PageResult<AttendanceDO> pr = attendanceAO.queryPage(page, queryVO);
		if (pr.isSuccess()) {
			andView.setViewName("erp/attendanceList");
			andView.addObject("queryVO", queryVO);
			andView.addObject("statusMap", DictionaryUtils.ATTEND);
			andView.addObject("pr", pr);
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 进入出勤记录 编辑页面
	 * </pre>
	 * 
	 * @param attendanceId
	 * @return
	 */
	@GetMapping(value = "edit/{attendanceId}")
	public ModelAndView edit(@PathVariable Integer attendanceId) {
		ModelAndView andView = new ModelAndView();

		Resp<AttendanceDO> resp = attendanceAO.queryById(attendanceId);
		if (resp.isSuccess()) {
			andView.setViewName("erp/attendanceEdit");
			andView.addObject("attendanceDO", resp.getData());
		} else {
			andView.setViewName("error");
		}
		return andView;
	}

	/**
	 * <pre>
	 * 编辑保存出勤记录信息
	 * </pre>
	 * 
	 * @param attendanceId
	 * @return
	 */
	@PostMapping(value = "edit")
	@ResponseBody
	public Resp<Integer> edit(AttendanceDO attendanceDO) {
		return attendanceAO.updateAttendance(attendanceDO);
	}

}
